<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无怖(285753421@qq.com)
 * Date: 2015/5/31
 * Time: 12:09
 */

class Pay extends MY_Pc_Api_Controller{
    protected $pay_config=array();
    function __construct(){
        parent::__construct();
        $this->load->config('pay');

    }

    function alipay($order_id){
        $this->load->model('order/Orders_model');
        $this->load->model('activities/Activities_model');
        $order = $this->Orders_model->getOne(array('order_id'=>$order_id));
        $activity = $this->Activities_model->getOne(array('id'=>$order_id['activity_id']));
        $pay_config = $this->config->item('alipay');
        $detector = new Detection\MobileDetect();
        $is_mobile = $detector->isMobile();
        $alipay = new \mytharcher\sdk\alipay\Alipay($pay_config,$is_mobile);
        if ($is_mobile) {
            $params = $alipay->prepareMobileTradeData(array(
                'out_trade_no'      => $order['order_id'],
                'subject'           => 'PaintMore 绘陌-订单',
                'total_fee'         => $order['price'],
                'body'              => $activity['title'],
                'merchant_url' => 'http://'.$_SERVER['HTTP_HOST'],
                'req_id'       => date('Ymdhis-',time())
            ));
            echo $alipay->buildRequestFormHTML($params, 'get');
        } else {
            echo $alipay->buildRequestFormHTML(array(
                "service"       => "create_direct_pay_by_user",
                "partner"       => trim($pay_config['partner']),
                "payment_type"  => $pay_config['payment_type'],
                "notify_url"    => $pay_config['notify_url'],
                "return_url"    => $pay_config['return_url'],
                "seller_id"     => $pay_config['partner'],
                'out_trade_no'  => $order['order_id'],
                'subject'       => $this->config->item('subject'),
                'total_fee'     => $order['price'],
                'body'           => $activity['title'],
                "show_url"       => site_url('activity').'?activity_id='.$activity['id'],
                "anti_phishing_key" => '',
                "exter_invoke_ip"   => '',
                "_input_charset"    =>$pay_config['input_charset']
            ), "post");
        }
    }

    function notify($payfunc){
        if($payfunc=='alipay'){
            $this->load->config('pay');
            $pay_config = $this->config->item('alipay');
            $alipay = new \mytharcher\sdk\alipay\Alipay($pay_config);
            $result = $alipay->verifyCallback(TRUE);
            $input = $this->input->post();
            if ($result && ($input['trade_status'] == 'TRADE_FINISHED' || $input['trade_status'] == 'TRADE_SUCCESS')) {
                $order_id   = $input['out_trade_no'];//订单号
                $serial     = $input['trade_no']; //支付宝交易流水
                $this->load->model('order/Orders_model');
                if($this->Orders_model->get_status($order_id)===NULL)
                {
                    $this->Orders_model->order_payed($order_id,$serial);
                    log_message('info',date('Y-m-d H:i:s',time()).':'.$order_id.'支付成功');
                }
                echo 'success';
            } else {
                log_message('error','支付失败'.date('Y-m-d H:i:s',time()));
                $this->json_response(false);
            }

        }
    }
    function return_url($payfunc){
        if($payfunc=='alipay'){
            $this->load->config('pay');
            $pay_config = $this->config->item('alipay');
            $alipay = new \mytharcher\sdk\alipay\Alipay($pay_config);
            $result = $alipay->verifyCallback();
            $input = $this->input->get();
            if ($result && ($input['trade_status'] == 'TRADE_FINISHED' || $input['trade_status'] == 'TRADE_SUCCESS')) {
                $order_id   = $input['out_trade_no'];//订单号
                $serial     = $input['trade_no']; //支付宝交易流水
                $this->load->model('order/Orders_model');
                if($this->Orders_model->get_status($order_id)===NULL)
                {
                    $this->Orders_model->order_payed($order_id,$serial);
                    log_message('info',date('Y-m-d H:i:s',time()).':'.$order_id.'支付成功');
                }
                //todo:跳转到支付成功的页面
                redirect(site_url());
            } else {
                //todo:跳转到支付成功的页面
                redirect(site_url());
            }
        }
    }

}